﻿using System;
using System.ComponentModel;

namespace zielonka.co.uk.extensions.Generic.Class.generic
{
    // Description:	Returns true if the type can be considered numeric
    /*
        if (someType.IsNumeric())
        {
            do something that requires type be numeric
        }
      */
    public static partial class GenericExtensions
    {
        public static bool IsNumeric(this Type t)
        {
            var type = t.GetTypeWithoutNullability();
            return
                type == typeof(Int16) ||
                type == typeof(Int32) ||
                type == typeof(Int64) ||
                type == typeof(UInt16) ||
                type == typeof(UInt32) ||
                type == typeof(UInt64) ||
                type == typeof(decimal) ||
                type == typeof(float) ||
                type == typeof(double);
        }

        public static Type GetTypeWithoutNullability(this Type t)
        {
            return t.IsNullable() ? new NullableConverter(t).UnderlyingType : t;
        }

        public static bool IsNullable(this Type t)
        {
            return t.IsGenericType &&
                   t.GetGenericTypeDefinition() == typeof(Nullable<>);
        }

    }
}
